<link type="text/css" rel="stylesheet" href="http://www.12301.cc/tpl/publish_coupons/css/publish_coupons.css" />
<link rel="stylesheet" href="http://www.12301.cc/css/common/www.12301.cc.global.css"/>
<script type="text/javascript" src="http://www.12301.cc/js/global/www.12301.cc.global.js"></script>
<div class="publish_manage">
	<ul id="tabHead" class="tabHead clearfix">
        <li class="profit">优惠券管理</li>
    </ul>
	<div class="formIn" id="searchWrap">
		<input type="text" class="formInp" id="serhInp" value="">
		<span class="formInp_btn serhBtn" id="serhBtn" >搜索</span>
	</div>
	<span class="line"></span>
	<div class="pub_man" id="dmlistUI" value="">
		<div class="pub_top">
			<div class="pub_top_left" data_li="0">
				<a href="javascript:void(0);" class="pub_normal nou">正常</a>
				<a href="javascript:void(0);" class="pub_history">历史记录</a>
			</div>
			<div class="pub_top_right">
				<i class="iconfont pub_icon">&#xe647;</i>
				<a href="/publish_coupons.html" target="_blank" class="pub_publish">发布优惠券</a>
			</div>
			<div class="listWrap" id="listWrap">
				<table class="tbCount setMT_20 " id="mlistUl"  border="0" cellpadding="0" cellspacing="0"></table>
				<div class="pagenavW clearfix" id="pagenavW">
					<div class="rt">
						<div class="d d2">
							<span id="total_num"></span>
							<a id="prevPageBtn" href="javascript:void(0)" data-first="" data-last=""  data-mid="" class="prevBtn btn pageBtn disable" style="margin-left:10px;">上一页</a>
							<a id="nextPageBtn" href="javascript:void(0)" class="nextBtn btn pageBtn">下一页</a>
						</div>
					</div>
				</div> 
            </div>
		</div>
	</div>
</div>
<div class="bgbox">	</div>
<div class="checkchoose_box " id="checkchoose_box">
		<div class="theme_top">
			<p class="ticket_name_ch select_i" data_w="" data_idd="">适用产品</p>
		</div>
		<div class="theme_select">
			<!-- <label class="labeld">
				<input type="radio" name="chose_" value="0" class="chose_all" checked="checked">
				<span class="theme_t">所以产品</span>
			</label> -->
			<label class="labeld">
				<input type="radio" name="chose_" value="1" class="chose_se" checked="checked">
				<span class="theme_t">选择产品</span>
			</label> 
		</div>
		<div class="selectbox">
			<ul id="prods_list" class="ul_list">
				<ul id="selected_lnk_prods"></ul>
			</ul>
		</div>
		<div class="sereize">
			<div class="qunaer_select qunaer_type" id="qunaer_select">
				<input id="prodSearchInp" class="prodSearchInp"  type="text"></input>
				<i class="iconfont select_icon">&#xe633;</i>
				<ul class="prodList" id="prodList"></ul>
				<div class="searchmsg"><span class="no_seach">无结果</span></div>
				<input type="hidden"  id="lid_id" value=""/>
				<div class="qunaer_table">
					<div class="tbBox">	</div> 
				</div>
			</div>
			
			<div class="order_div" id="order_lili">
				<ul class="order_ul"></ul>
			</div>
		</div>
		<div class="btnbox">
			<span class="btn_base btn_cancels">取消</span>
			<span class="btn_base sure_s btn_sure_all" >确定</span>
		</div>
</div>
<script type="text/javascript" src="http://www.12301.cc/tpl/prod_sale/js/class.js"></script>
<script type="text/javascript" src="http://wx.12301.cc/public/js/common/ajax.js"></script>
<script type="text/javascript" src="http://www.12301.cc/tpl/publish_coupons/js/publish_coupons.js"></script>
<script id="list_template" type="text/template">
	<li class="list">
		<span class="title_list" title="{title}">
		{title}
		</span>
		<a class="rm" title="删除" href="javascript:;">X</a>
		<input type="hidden" name="lnkprods[]" value="{id}">
	</li>
</script>
<script type="text/javascript" src="http://www.12301.cc/tpl/publish_coupons/js/js/ajaxData.js"></script>
<script type="text/javascript" src="http://www.12301.cc/tpl/publish_coupons/js/js/sData.js"></script>
<!-- <script type="text/javascript" src="http://www.12301.cc/tpl/prod_sale_pro/js/selected.js"></script> -->
<script type="text/javascript" src="http://www.12301.cc/tpl/prod_sale/js/pager.js"></script>
<script type="text/javascript" src="http://www.12301.cc/tpl/publish_coupons/js/js/search.js"></script>
<script type="text/javascript" src="http://www.12301.cc/tpl/publish_coupons/js/js/product_package.js"></script>
<script type="text/javascript" src="http://www.12301.cc/tpl/publish_coupons/js/publish_manage.js"></script>
<script>
jQuery(document).ready(function($){
	$(".user").live('click', lnk_prods_opt.check_data);
	$(".user").live('click', lnk_prods_opt.showDialog);
    $(".add_list").live('click', lnk_prods_opt.chlick);
	//$(".btn_sure_all").live('click', lnk_prods_opt.add_list);
});
var lnk_prods_opt = {
	check_data : function(e){
		var target = $(e.currentTarget); 
		var lids = target.attr("idd");
		var data = {id:lids}; 
		var url = "/call/jh_coupon.php?action=coupon_list";
			PFT.Ajax({
				url : url,
				type : "GET",  
				dataType : "json",
				data : data
			},function(res){
				var da = ""; 
				var lists = res.list;
				for(var i in lists){
					var da = lists[i]["pid"];//适用产品	
					target.attr("data_id",da)
				} 
			})
	},
    showDialog : function(e){
		var h=$(window).height(); 
		var w=$(window).width(); 
		var target = $(e.currentTarget); 
		var tpl = $("#list_template").html();
		var idd = target.attr("idd");
		lnk_prods_opt.check_data(e,idd);
		$(".select_i").attr("data_idd",idd)
		var ttitle = target.attr("title"); 
		$(".ticket_name_ch").text(ttitle);
		$(".bgbox").show();
		$("#checkchoose_box").show();
		$("#checkchoose_box").animate({"top":100});
		$(".bgbox").css("height",h);
		$(".bgbox").css("width",w); 
		$(".btn_cancels,.close_btn").on("click",function(e){
			$(".ul_list li").each(function(){
				$(this).remove();
			})
			var target = $(e.currentTarget);
			$("#checkchoose_box").animate({"top":-100});			
			$(".bgbox,#checkchoose_box").hide();				
		});
		$(".btn_sure_all").on("click",function(){
				lnk_prods_opt.clikkk()
				$("#checkchoose_box").animate({"top":-100});			
				$(".bgbox,#checkchoose_box").hide();
				var s = $(".select_i").attr("data_w");
				var id = $(".select_i").attr("data_idd");
				if (s.substr(0,1)==',') 
				s=s.substr(1);
				//console.log("ddd"+s)
				var data = {
					action:"coupon_pid_relation",
					id : id,
					pid : s
				};
				$.ajax({
					type:'POST',url: 'http://www.12301.cc/call/jh_coupon.php',data: data, dataType:'json',
				}).done(function(res) {
					if(res.status=="success"){
						target.attr("data_id",s);
						PFT_GLOBAL.U.Alert("success",'<p style="width:240px">'+res.msg+'</p>');
					}
					else{
						PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">'+res.msg+'</p>');
					}
				})
				$(".select_i").val(s)
				$(".ul_list li").each(function(){
					$(this).remove();
				})
		})
		var objtr =target.attr("data_id");
		var  tpl = $("#list_template").html();
		$("#selected_lnk_prods").empty();
		var le = objtr.split(",")
		var len = le.length;
		for(var i=0;i<len;i++)
		 {
			var id = $(le)[i];
			//var id = lids;
			var data = {pid:id};
			var url = "/call/jh_coupon.php?action=coupon_prods";
			PFT.Ajax({
				url : url,
				type : "GET",
				dataType : "json",
				data : data
			},function(res){
				var lists = res.list;
				for(var i in lists){
					var t = lists[i]["pid"];
					var ltitle = lists[i]["ltitle"];
					var ttitle = lists[i]["ttitle"];
					var k = ltitle+"-"+ttitle;
					$("#selected_lnk_prods").after(template(tpl, {id:t,title:k}));
				}
			})
		 }
    },
	chlick : function(){
				var  tpl = $("#list_template").html();
				$("#selected_lnk_prods").empty();
					var kk = $(this).attr("title");
					var tttile = $(this).attr("tttile");
					var k = kk+"-"+tttile;
					var lig = $(".ul_list li").length;
					var id = $(this).attr("data-id");
					if(lnk_prods_opt.check(id)){
						  $("#selected_lnk_prods").after(template(tpl, {id:id,title:k}));
					}
					 else{
						PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">您已添加了同样的产品</p>');
					 }	
			},
	check : function(id){
		var re = true;
		$(".ul_list li input").each(function(){
			var data = $(this).val();
				if(id==data){
					re = false;
				}
		})
		return re;
	},
	clikkk :function(){
		var sel="";
		$(".ul_list li input").each(function(){		
			sel+= ","+$(this).val();  
			$(".select_i").attr("data_w",sel)    
		})
	},
	add_list : function(){
		lnk_prods_opt.clikkk()
		$("#checkchoose_box").animate({"top":-100});			
		$(".bgbox,#checkchoose_box").hide();
		var s = $(".select_i").attr("data_w");
		var id = $(".select_i").attr("data_idd");
		if (s.substr(0,1)==',') 
		s=s.substr(1);
		console.log("ddd"+s)
		var data = {
			action:"coupon_pid_relation",
			id : id,
			pid : s
		};
		$.ajax({
			type:'POST',url: 'http://www.12301.cc/call/jh_coupon.php',data: data, dataType:'json',
		}).done(function(res) {
			if(res.status=="success"){
				PFT_GLOBAL.U.Alert("success",'<p style="width:240px">'+res.msg+'</p>');
			}
			else{
				PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">'+res.msg+'</p>');
			}
		})
		$(".select_i").val(s)
		$(".ul_list li").each(function(){
			$(this).remove();
		})
	}
};
function template(tpl, val){
	return tpl.replace(/\{(\w+)\}/g, function(m,i){
		if(val[i]) {
			return val[i];
		} else {
			return '';
		}
	})
};
$(".rm").live('click',function(e){
	$(this).parent().remove();
	var v = $(this).next('input').val();
	$("input[name='lnkprods'][value='"+v+"']").remove();
})
var sel="";
$(".btn_sure_allk").on('click',function(e){
	$(".ul_list li input").each(function(i){		
		sel+= $(this).val()+",";  
		console.log("se"+sel)
		$(".select_ic").val(sel)
		$("#checkchoose_box").animate({"top":-100});			
		$(".bgbox,#checkchoose_box").hide();
	})
}) 
$(".pub_history").click(function(){
	$(this).addClass("nou");
	$(".pub_normal").removeClass("nou");
})
$(".pub_normal").click(function(){
	$(this).addClass("nou");
	$(".pub_history").removeClass("nou");
})
</script>